package com.shenzhen.horselive.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.GlideHelper;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.shenzhen.horselive.Constants;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.MeAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.BaseFragment;
import com.shenzhen.horselive.beans.IsBindBean;
import com.shenzhen.horselive.beans.MeFragmentItemBean;
import com.shenzhen.horselive.beans.MyCityBean;
import com.shenzhen.horselive.databinding.FragmentMeBinding;
import com.shenzhen.horselive.databinding.HeadItemMeBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.BaseView;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.activity.AddrListActivity;
import com.shenzhen.horselive.ui.activity.CityPartnerActivity;
import com.shenzhen.horselive.ui.activity.EmptyActivity;
import com.shenzhen.horselive.ui.activity.FeedbackActivity;
import com.shenzhen.horselive.ui.activity.KeFuWeChatActivity;
import com.shenzhen.horselive.ui.activity.MyCityActivity;
import com.shenzhen.horselive.ui.activity.MyTeamActivity;
import com.shenzhen.horselive.ui.activity.RealNameAuthActivity;
import com.shenzhen.horselive.ui.activity.SendReciRedDetailsActivity;
import com.shenzhen.horselive.ui.activity.SettingActivity;
import com.shenzhen.horselive.ui.activity.UserInfoActivity;
import com.shenzhen.horselive.ui.activity.WebActivity;

import java.util.ArrayList;
import java.util.List;

import kotlin.jvm.internal.Ref;

public class MeFragment extends BaseFragment {


    public  static final  int  REQUSET =1000;
    public static MeFragment newInstance() {
        MeFragment fragment = new MeFragment();
        return fragment;
    }
    FragmentMeBinding  binding;
    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup viewGroup) {
        binding = FragmentMeBinding.inflate(getLayoutInflater(),viewGroup,false);
        initViews();
        return binding.getRoot();
    }

    @Override
    protected void loadDataOnece() {
        super.loadDataOnece();
        initData();
    }
    private  void   initData()
    {
        ApiFactory.getApiService().getUserInfo(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<IsBindBean>(this) {
                    @Override
                    protected void onNextInActive(IsBindBean bean) {
                        if (binding.smartRefresh.isRefreshing())
                        binding.smartRefresh.finishRefresh();
                        if (bean.isSuccess()&&bean.getData()!=null)
                        {
                            User.get().storeMyInfo(bean.getData());
                            if (bean.getData().getRealNameStatus()==1)
                            {
                                meAdapter.getData().get(0).setText("已实名");
                                meAdapter.notifyItemChanged(1);
//                                meAdapter.notifyItemChanged(0,new MeFragmentItemBean(R.mipmap.ico_me_item0,"实名认证 ","已实名"));
                            }
                            else
                            {
                                meAdapter.getData().get(0).setText("未实名");
                                meAdapter.notifyItemChanged(1);
//                                meAdapter.notifyItemChanged(0,new MeFragmentItemBean(R.mipmap.ico_me_item0,"实名认证 ","未实名"));
                            }
                            binding.tvName.setText(bean.getData().getNickname());
                            binding.tvInviteName.setText("推荐人："+bean.getData().getPreUserPhone());
                            GlideHelper.loadAdvertImage(bean.getData().getHeadimgurl(),binding.imgUser);
                            binding.tvGrade.setText(bean.getData().getGradeName());
                        }else
                        {
                            showToast(bean.getErrMessage());
                        }
                    }
                });

    }

    MeAdapter  meAdapter;
    private void initViews() {
        List<MeFragmentItemBean>  list = new ArrayList<>();
        list.add(new MeFragmentItemBean(R.mipmap.ico_me_item0,"实名认证 ",""));
        //我的团队
        list.add(new MeFragmentItemBean(R.mipmap.ico_my_team,"我的团队 ",""));
//      list.add(new MeFragmentItemBean(R.mipmap.ico_me_item1,"黑名单 ",""));
        list.add(new MeFragmentItemBean(R.mipmap.ico_me_item2,"收货地址 ",""));
        //常见问题
        list.add(new MeFragmentItemBean(R.mipmap.ico_setting1,"常见问题 ",""));
        list.add(new MeFragmentItemBean(R.mipmap.ico_me_item3,"联系客服 ",""));

        //bug反馈
        list.add(new MeFragmentItemBean(R.mipmap.ico_setting2,"BUG反馈 ",""));

        list.add(new MeFragmentItemBean(R.mipmap.ico_me_item4,"设置 ",""));
        meAdapter=  new MeAdapter(R.layout.item_me,list);
        meAdapter.addHeaderView(getHead());
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerview.setAdapter(meAdapter);

        binding.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getContext(), UserInfoActivity.class),REQUSET);
            }
        });
     meAdapter.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
             switch (position)
             {
                 case 0:
                     if (User.get().getMyInfo().getRealNameStatus()!=1)
                     {
                     startActivityForResult(new Intent(getContext(), RealNameAuthActivity.class),REQUSET);
                     }
                     else
                     {
                         showToast("已经实名");
                     }
                     break;
                 case 2:
                     //收货地址管理
                     startActivity(new Intent(getContext(), AddrListActivity.class));
                     break;
                 case 4:
                     //联系客服
                     startActivity(new Intent(getContext(), KeFuWeChatActivity.class));
                     break;
                 case 5:
                     //bug反馈
                     //意见反馈
                     startActivity(new Intent(getContext(), FeedbackActivity.class));
                     break;
                 case 6:
                     startActivity(new Intent(getContext(), SettingActivity.class));
                     break;
                 case 1:
                     //我的团队
                     startActivity(new Intent(getContext(), MyTeamActivity.class));
                     break;
                 case 3:
                     //常见问题
                     WebActivity.start(getContext(), Constants.H5_COM_PROBLEMS,"常见问题");
                     break;
             }
         }
     });

//     binding.smartRefresh.setEnableScrollContentWhenLoaded(false);//是否在加载完成时滚动列表显示新的内容
     binding.smartRefresh.setOnRefreshListener(new OnRefreshListener() {
         @Override
         public void onRefresh(@NonNull RefreshLayout refreshLayout) {
             initData();
         }
     });
     binding.smartRefresh.setEnableLoadMore(false);
    }

    private View getHead() {
        HeadItemMeBinding   binding =  HeadItemMeBinding.inflate(getLayoutInflater());
        binding.linerAboutRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //红包动态
                startActivity(new Intent(getContext(), SendReciRedDetailsActivity.class));
            }
        });
        //我的主城
        binding.linerMycity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToMycity();
//                startActivity(new Intent(getContext(), MyCityActivity.class));
            }
        });
        //我的卡券
        binding.linerCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EmptyActivity.start(getContext(),"我的卡券");
            }
        });
        return binding.getRoot();
    }

    //查询是否有主城
    private void jumpToMycity() {
        ApiFactory.getApiService().mycitylist(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<MyCityBean>(this) {
                    @Override
                    protected void onNextInActive(MyCityBean myCityBean) {
                        if (myCityBean.isSuccess())
                        {
                            if (myCityBean.getData()==null||myCityBean.getData().size()==0)
                            {
                                //还没有主城
                                CityPartnerActivity.start(getContext(),1);
                            }else
                            {
                                //有主城
                                startActivity(new Intent(getContext(),MyCityActivity.class));
                            }
                        }else
                        {
                            showToast(myCityBean.getErrMessage());
                        }
                    }
                });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUSET&&resultCode==1001)
        {
            //实名完成之后更新数据
            initData();
        }
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.red_common)
                .statusBarDarkFont(false)
                .init();
    }
}